using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

namespace ShiQuan.Security
{
    /// <summary>
    ///  DES全称为Data Encryption Standard，即数据加密标准，是一种使用密钥加密的块算法。
    /// </summary>
    public static class DESSecurity
    {
        #region Des 加密、解密

        //DES加密秘钥，要求为8位  
        private static readonly string key = "ShiQuan$";
        /// <summary>
        /// DES数据加密
        /// </summary>
        /// <param name="targetValue">目标值</param>
        /// <returns>加密值</returns>
        public static string EncryptDES(this string input)
        {
            return Encrypt(input, key);
        }
        /// <summary>
        /// DES数据加密
        /// </summary>
        /// <param name="input">目标值</param>
        /// <param name="key">密钥</param>
        /// <returns>加密值</returns>
        public static string EncryptDES(this string input, string key)
        {
            return Encrypt(input, key);
        }
        /// <summary>
        /// DES 数据加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string Encrypt(string input)
        {
            return Encrypt(input, key);
        }
        /// <summary>
        /// DES数据加密
        /// </summary>
        /// <param name="input">目标值</param>
        /// <param name="key">密钥</param>
        /// <returns>加密值</returns>
        public static string Encrypt(string input, string key)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(input);
                des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Security.Encrypt(key).ToUpper().Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Security.Encrypt(key).ToUpper().Substring(0, 8));

                System.IO.MemoryStream mStream = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(mStream, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                /*2019-06-28 侯连文 转码有问题*/
                //return Encoding.UTF8.GetString(mStream.ToArray());
                StringBuilder result = new StringBuilder();
                foreach (byte b in mStream.ToArray())
                {
                    result.AppendFormat("{0:X2}", b);
                }
                return result.ToString();
            }
            catch
            {
                return input;
            }
        }


        #endregion

        #region 解密
        /// <summary>
        /// DES数据解密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string DecryptDES(this string input)
        {
            return Decrypt(input, key);
        }
        /// <summary>
        /// DES数据解密
        /// </summary>
        /// <param name="targetValue"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DecryptDES(this string input, string key)
        {
            return Decrypt(input, key);
        }
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string Decrypt(string input)
        {
            return Decrypt(input, key);
        }
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="input"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Decrypt(string input, string key)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len = input.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(input.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Security.Encrypt(key).ToUpper().Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Security.Encrypt(key).ToUpper().Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            /*2019-06-28 侯连文 中文返回乱码*/
            //return Encoding.UTF8.GetString(ms.ToArray());
            return Encoding.Default.GetString(ms.ToArray());
        }

        
        #endregion
    }
}
